import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Optional;

/**
 * @author pzy
 * @date 2024/6/18
 */
public class OptionalDemo {
    public static void main(String[] args) {
        String name = getName(null);
        System.out.println("name = " + name);
    }

    public static String getName(User u) {
        return Optional.ofNullable(u).map(user -> user.getName()).orElse("Unknown!");
    }
}

@Data
@NoArgsConstructor
class User implements Serializable {
    private String name;
    private int age;
}
